package cn.com.gas.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import cn.com.common.constants.Constants;
import cn.com.common.result.DataResult;
import cn.com.gas.entity.GasAnalyseRegionRankingEntity;
import cn.com.gas.entity.GasForecastEntity;
import cn.com.gas.mapper.GasAnalyseRegionRankingMapper;
import cn.com.gas.service.GasForecastService;
import cn.com.system.service.impl.HttpSessionService;

/**
 * 预估值记录
 *
 * @author shaochangyang
 * @email l2l84o7o5@live.com
 * @date 2024-04-22 20:58:51
 */
@Controller
@RequestMapping("/gas/forecast")
public class GasForecastController {

	@Autowired
	private GasForecastService gasForecastService;
	@Autowired
	private GasAnalyseRegionRankingMapper gasAnalyseRegionRankingMapper;
	@Autowired
	private HttpSessionService httpSessionService;

	@ApiOperation(value = "新增")
	@PostMapping("/add")
	@ResponseBody
	public DataResult add(@RequestBody GasForecastEntity gasForecast) {

		String currentDeptId = httpSessionService.getCurrentDeptId();

		// 执行查询并获取结果

		QueryWrapper<GasAnalyseRegionRankingEntity> queryWrapper = new QueryWrapper<>();
		LambdaQueryWrapper<GasAnalyseRegionRankingEntity> lambdaQueryWrapper = queryWrapper
				.select("ROUND(sum(sale),2) as sale,year_months as yearMonths").lambda()
				.groupBy(GasAnalyseRegionRankingEntity::getYearMonths);

		// lambdaQueryWrapper.select(GasAnalyseRegionRankingEntity::getSale);  指定要查询的字段
		lambdaQueryWrapper.likeRight(GasAnalyseRegionRankingEntity::getTaxorgcode, currentDeptId.substring(0, 5));
		List<GasAnalyseRegionRankingEntity> results = gasAnalyseRegionRankingMapper.selectList(queryWrapper);

		// 初始值为0
		BigDecimal sum = new BigDecimal("0");
		for (GasAnalyseRegionRankingEntity result : results) {
			sum = sum.add(new BigDecimal(result.getSale()));
		}

		LambdaQueryWrapper<GasForecastEntity> deleteWrapperGasForecast = new LambdaQueryWrapper<>();
		gasForecastService.remove(deleteWrapperGasForecast);

		// gasForecast.setNumerical(String.valueOf(sum.multiply(new
		// BigDecimal("6"))));

		gasForecast.setNumerical(String.valueOf(sum.divide(new BigDecimal(results.size()), 2)));

		gasForecast.setCreateId(httpSessionService.getCurrentUserId());
		gasForecast.setDeleted(Constants.DELETEED_NOT);
		gasForecast.setCreateTime(new Date());

		gasForecastService.save(gasForecast);
		return DataResult.success();
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@ApiOperation(value = "查询分页数据")
	@PostMapping("/list")
	@ResponseBody
	public DataResult findListByPage(@RequestBody GasForecastEntity gasForecast) {
		Page page = new Page(gasForecast.getPage(), gasForecast.getLimit());
		LambdaQueryWrapper<GasForecastEntity> queryWrapper = Wrappers.lambdaQuery();
		// 查询条件示例
		// queryWrapper.eq(GasForecastEntity::getId, gasForecast.getId());
		IPage<GasForecastEntity> iPage = gasForecastService.page(page, queryWrapper);
		return DataResult.success(iPage);
	}

}
